package com.pro_test.proservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pro_test.commonutils.R;
import com.pro_test.proservice.entity.ProComplaint;
import com.pro_test.proservice.entity.ProTemporarypark;
import com.pro_test.proservice.entity.vo.ComplaintQuery;
import com.pro_test.proservice.service.ProComplaintService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Captain
 * @since 2022-02-26
 */
@RestController
@RequestMapping("/proservice/pro-complaint")
@CrossOrigin
public class ProComplaintController {
    @Autowired
    private ProComplaintService proComplaintService;

    //查询投诉表所有数据
    @ApiOperation(value = "查询所有投诉")
    @GetMapping("findAll")
    public R findAllComplaint()
    {
        List<ProComplaint> list = proComplaintService.list(null);
        return R.ok().data("items",list);
    }

    //逻辑删除投诉
    @ApiOperation(value = "逻辑删除投诉")
    @DeleteMapping("{id}")
    public R removeComplaint(@ApiParam(name = "id", value = "投诉ID", required = true)
                         @PathVariable String id)
    {
        boolean flag = proComplaintService.removeById(id);
        if (flag)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

    //条件查询分页
    @ApiOperation(value = "条件分页查询")
    @PostMapping("pageComplaintCondition/{current}/{limit}")
    public R pageComplaintCondition(@PathVariable long current,
                                @PathVariable long limit,
                                @RequestBody(required = false) ComplaintQuery complaintQuery)
    {
        //创建page对象
        Page<ProComplaint> pageComplaint = new Page<>(current,limit);

        //构建条件
        QueryWrapper<ProComplaint> wrapper = new QueryWrapper<>();
        Integer status = complaintQuery.getStatus();
        String handler = complaintQuery.getHandler();
        String begin = complaintQuery.getBegin();
        String end = complaintQuery.getEnd();

        if (!StringUtils.isEmpty(handler))
        {
            wrapper.like("handler",handler);
        }
        if (!StringUtils.isEmpty(status))
        {
            wrapper.eq("status",status);
        }
        if (!StringUtils.isEmpty(begin))
        {
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end))
        {
            wrapper.le("gmt_create",end);
        }
        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        proComplaintService.page(pageComplaint,wrapper);
        long total = pageComplaint.getTotal();
        List<ProComplaint> records = pageComplaint.getRecords();

        return R.ok().data("total",total).data("items",records);
    }

    //添加投诉
    @ApiOperation(value = "添加投诉")
    @PostMapping("addComplaint")
    public R addComplaint(@RequestBody ProComplaint proComplaint)
    {
        boolean save = proComplaintService.save(proComplaint);
        if (save)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

    //根据投诉id进行查询
    @ApiOperation(value = "查询投诉")
    @GetMapping("getComplaint/{id}")
    public R getComplaint(@PathVariable String id)
    {
        ProComplaint proComplaint = proComplaintService.getById(id);
        return R.ok().data("complaint",proComplaint);
    }

    @GetMapping("getComplaintByOwnerId/{ownerId}")
    public R getComplaintByOwnerId(@PathVariable String ownerId)
    {
        Page<ProComplaint> pageComplaint = new Page<>();

        QueryWrapper<ProComplaint> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(ownerId))
        {
            wrapper.eq("owner_id",ownerId);
        }
        proComplaintService.page(pageComplaint,wrapper);
        long total = pageComplaint.getTotal();
        List<ProComplaint> records = pageComplaint.getRecords();

        return R.ok().data("total",total).data("proComplaint",records);
    }

    //修改投诉
    @ApiOperation(value = "修改投诉")
    @PostMapping("updateComplaint")
    public R updateComplaint(@RequestBody ProComplaint proComplaint)
    {
        boolean flag = proComplaintService.updateById(proComplaint);
        if (flag)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }
}

